package com.neusoft.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.dao.RentDao;
import com.neusoft.entity.Rent;
import com.neusoft.service.RentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Service("rentService")
public class RentServiceImpl implements RentService {
    @Resource
    private RentDao rentDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Rent queryById(Integer id) {
        return this.rentDao.queryById(id);
    }

    @Override
    public Rent queryByHouseId(Integer houseid) {
        return rentDao.queryByHouseId(houseid);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public IPage<Rent> queryAllByLimit(int offset, int limit, Rent bean) {
        Page<Rent> page = new Page<>(offset,limit);
//        QueryWrapper<Student> wrapper = new QueryWrapper<>();
//        wrapper.like("account",bean.getAccount())
//                .eq("mobile",bean.getMobile()).
//                gt("reg_date",bean.getStartTime()).
//                lt("reg_date",bean.getEndTime());
        page.setRecords(rentDao.queryAll(page,bean));
        return page;
    }

    @Override
    public List<Rent> queryAll(Rent bean) {
        return null;
    }


    /**
     * 新增数据
     *
     * @param rent 实例对象
     * @return 实例对象
     */
    @Override
    public Rent insert(Rent rent) {
        this.rentDao.insert(rent);
        return rent;
    }

    /**
     * 修改数据
     *
     * @param rent 实例对象
     * @return 实例对象
     */
    @Override
    public int update(Rent rent) {
        return this.rentDao.update(rent);
    }

    @Override
    public boolean deleteById(List<Integer> ids) {
        System.out.println("执行批量删除");
        if(ids == null || ids.size()==0){
            return false;
        }
        StringBuffer sb = new StringBuffer("id in (");
        for(Integer item:ids){
            sb.append("'");
            sb.append(item);
            sb.append("',");
        }
        sb.deleteCharAt(sb.lastIndexOf(","));
        sb.append(")");
        return this.rentDao.delete("stu.rent",sb.toString())>0;
    }

    @Override
    public boolean deleteById1(Integer id) {
        if(id == null){
            return false;
        }
        StringBuffer sb = new StringBuffer("id =");
        sb.append(id);
        System.out.println(sb.toString());
        return this.rentDao.delete1("stu.rent",sb.toString())>0;
    }

    @Override
    public boolean deleteByHouseId(Integer houseid) {
        return rentDao.deleteByHouseId(houseid)>0;
    }

}
